// pages/public/register/register.js
const http = require("../../../utils/httpUtils.js");
const app = getApp();
const RSA = require("../../../utils/wx_rsa.js");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '账号注册' //导航栏 中间的标题
        },
        send: true,
        second: 60,
        agree: false,
        opacity: 0.5,
        height: app.globalData.height * 2 + 20
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },
    // 选中协议
    checkboxChange(e) {
        e.detail.value[0] == "agree" ? this.data.agree = true : this.data.agree = false
    },
    signIn() {
        // redirectTo 关闭当前页面
        wx.redirectTo({
            url: '/pages/public/login/login',
        })
    },
    inPhone(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    // 发送验证码
    sendMsg: function() {
        if (this.data.phone == undefined) {
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                mask: true
            })
            return
        }
        if (this.data.phone.length != 11) {
            wx.showToast({
                title: '手机号格式错误',
                icon: 'none'
            })
            return
        }
        http._get('/api/sendsms/sendsms', {
            phone: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(this.data.phone)),
            scene: 'register',
            client: 'weixinSmall'
        }, res => {
            if (res.data.code == 1) {
                wx.showToast({
                    title: '获取成功请稍等',
                    icon: 'none'
                })
                this.setData({
                    send: false
                })
                this.timer()
            } else {
                wx.showModal({
                    title: '提示',
                    content: res.data.msg,
                    showCancel: false
                })
                return
            }
        })
    },
    formSubmit(e) {
        this.jiami();
    },
    //加密 注册
    jiami() {
        if (this.data.phone == undefined) {
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                mask: true
            })
            return
        }
        if (this.data.phone.length != 11) {
            wx.showToast({
                title: '手机号格式错误',
                icon: 'none'
            })
            return
        }
        // if (this.data.agree == false) {
        //     wx.showToast({
        //         title: '请阅读用户协议',
        //         icon: 'none'
        //     })
        //     return
        // }

        let phone = RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(this.data.phone));
        let data = {
            phone: phone,
            password: '',
            smsCode: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(this.data.smsCode)),
            client: 'weixinSmall'
        }

        http._post('/api/user/register', data, res => {
            if (res.data.code == 1) {
                wx.showModal({
                    title: '提示',
                    content: '注册成功',
                    showCancel: false,
                    success: res => {
                        let login = {
                            login_type: 'password',
                            phone: phone,
                            password: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(this.data.phone.substring(5))),
                            client: 'weixinSmall'
                        }
                        http._post('/api/user/login', login, res => {

                            if (res.data.code == 1) {
                                app.globalData.token = res.data.data.Authorization;
                                //保存token 
                                wx.setStorage({
                                    key: 'token',
                                    data: res.data.data,
                                    success: res => {
                                        wx.switchTab({
                                            url: "/pages/tabBar/specialty/specialty"
                                        })
                                    }
                                })
                            } else {
                                wx.showToast({
                                    title: res.data.msg,
                                })
                            }
                        })
                    }
                })
            } else {
                wx.showModal({
                    title: '提示',
                    content: res.data.msg,
                    showCancel: false
                })
            }

        })
    },
    //手机号聚焦
    phonefocus() {
        this.setData({
            inPhone: true
        })
    },
    //密码聚焦
    pwdfocus() {
        this.setData({
            pwd: true
        })
    },
    //手机号失焦
    phoneblur() {
        this.setData({
            inPhone: false
        })
    },
    //密码失焦
    pwdblur() {
        this.setData({
            pwd: false
        })
    },
    //监听输入时
    smsinput(event) {
        if (event.detail.cursor > 4) {
            this.setData({
                opacity: 1,
                smsCode: event.detail.value
            })
        } else {
            this.setData({
                opacity: 0.5
            })
        }
    },
    //倒计时
    timer() {
        let promise = new Promise((resolve, reject) => {
            let setTimer = setInterval(
                () => {
                    this.setData({
                        second: this.data.second - 1
                    })
                    if (this.data.second <= 0) {
                        this.setData({
                            second: 60,
                            send: true
                        })
                        resolve(setTimer)
                    }
                }, 1000)
        })
        promise.then((setTimer) => {
            clearInterval(setTimer)
        })
    }
})